package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.LocationBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_location_aty.*
import java.util.ArrayList

class LocationAty : BaseAty() {
    private val datas=ArrayList<LocationBean.DataBean>()
    private var isEdit=false
    private lateinit var adapter:BaseQuickAdapter<LocationBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int = R.layout.activity_location_aty
    override fun initView() {
        adapter=object :BaseQuickAdapter<LocationBean.DataBean,BaseViewHolder>(R.layout.location_item,datas){
            override fun convert(helper: BaseViewHolder?, item: LocationBean.DataBean?) {
                if(!isEdit){
                    helper?.setVisible(R.id.loEdit,false)
                    helper?.setVisible(R.id.dv,true)
                    val relativeLayout = helper?.getView<RelativeLayout>(R.id.loEdit)
                    val params = relativeLayout?.layoutParams
                    params?.height=0
                    relativeLayout?.layoutParams=params
                }else{
                    helper?.setVisible(R.id.loEdit,true)
                    helper?.setVisible(R.id.dv,false)
                    val relativeLayout = helper?.getView<RelativeLayout>(R.id.loEdit)
                    val params = relativeLayout?.layoutParams
                    params?.height=RelativeLayout.LayoutParams.WRAP_CONTENT
                    relativeLayout?.layoutParams=params
                }
                if(item?.is_def==1){
                    helper?.setVisible(R.id.isDefault,true)
                }else{
                    helper?.setVisible(R.id.isDefault,false)
                }
                helper?.setText(R.id.itemName,item?.name)
                helper?.setText(R.id.itenPhone,item?.mobile)
                helper?.setText(R.id.itemLocation,item?.province+item?.city+item?.area+item?.address)
                val btSetDefault = helper?.getView<TextView>(R.id.btSetDefault)
                val btDelet = helper?.getView<TextView>(R.id.btDelet)
                val btEdit = helper?.getView<TextView>(R.id.btEdit)
                btSetDefault?.setOnClickListener {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SETDEFAULT_LOCATION)
                            .params("id",datas[helper?.adapterPosition].id)
                            .execute(object :JsonCallBack<BaseBean>(this@LocationAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("设置成功")
                                        getNetData()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }
                btDelet?.setOnClickListener {
                    AlertDialog.Builder(this@LocationAty)
                            .setTitle("确认删除？")
                            .setPositiveButton("确认"){
                                dialog, which ->
                                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.DELET_LOCATION)
                                        .params("id",datas[helper?.adapterPosition!!].id)
                                        .execute(object :JsonCallBack<BaseBean>(this@LocationAty){
                                            override fun onSuccess(response: Response<BaseBean>?) {
                                                val bean = response?.body()
                                                if (bean?.code==1){
                                                    dialog.dismiss()
                                                    MyUtils.toast("删除成功")
                                                    getNetData()
                                                }else{
                                                    MyUtils.toast(bean?.msg)
                                                }
                                            }
                                        })
                            }
                            .setNegativeButton("取消"){
                                dialog, which ->
                                dialog.dismiss()
                            }.show()

                }
                btEdit?.setOnClickListener {
                    val intent = Intent(this@LocationAty, EditLocationAty::class.java)
                    intent.putExtra("id",datas[helper?.adapterPosition].id)
                    intent.putExtra("name",datas[helper?.adapterPosition].name)
                    intent.putExtra("phone",datas[helper?.adapterPosition].mobile)
                    intent.putExtra("loc","${datas[helper?.adapterPosition].province}${datas[helper?.adapterPosition].city}${datas[helper?.adapterPosition].area}")
                    intent.putExtra("detail","${datas[helper?.adapterPosition].area}")

                    startActivity(intent)
                }
            }

        }
        listView.adapter=adapter
        listView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)


        getNetData()
    }

    override fun onResume() {
        super.onResume()
        getNetData()

    }
    public fun getNetData() {
        OkGo.post<LocationBean>(Urls.BaseUrl + Urls.ADDRES_LIST)
                .execute(object : JsonCallBack<LocationBean>(this) {
                    override fun onSuccess(response: Response<LocationBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            datas.clear()
                            datas.addAll(bean?.data)
                            adapter.notifyDataSetChanged()
                        } else {
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
    }

    override fun initListener() {
        btManage.setOnClickListener {
            if (isEdit){
                isEdit=false
                adapter.notifyDataSetChanged()
            }else{
                isEdit=true
                adapter.notifyDataSetChanged()
            }
        }
        btAdd.setOnClickListener{
            val intent = Intent(this, EditLocationAty::class.java)
            intent.putExtra("type",1)
            startActivity(intent)
        }
        btExit.setOnClickListener {
            finish()
        }
    }
}
